
const CreateStation = require('./CreateStation');
const { CesiumFun } = require('./CesiumFun');
const Cesium = window.Cesium;
class SCesLayAdd {
    constructor() {

    }
    
    /**
     * @description 添加JSON图层
     * @author tpy
     * @author hjj
     * @date 2020-06-07
     * @param {Cesium.Viewer} viewer 
     * @param {Function} MChangeFun 回调1
     * @param {Function} callback 回调2
     * @memberof SCesLayAdd
     */
    TOPOJSONAdd(viewer, MChangeFun, callback) {
        try {
            var urls = window.URLSERVER;
            let MLayerObj = {};//MapLayers=[],
            let promises = [];
            // debugger;
            for (var i = 0; i < urls.length; i++) {
                let url = urls[i];
                let key = url.key
                try {
                    fetch(url.dataurl).then(response => response.json()).then(data => {
                        console.log("---------data--------",data)
                        promises[key] = Cesium.GeoJsonDataSource.load(data);
                        let mapcon = {
                            ParentKEY: url.ParentKEY,
                            title: url.title,
                            name: url.key,
                            show: url.show,
                            minH: url.minH,//最小可见高度
                            maxH: url.maxH//最大可见高度
                        };
                        // MapLayers.push(mapcon);
                        if (!MLayerObj[mapcon.ParentKEY]) {
                            MLayerObj[mapcon.ParentKEY] = [mapcon]
                        } else {
                            let temArr = MLayerObj[mapcon.ParentKEY];
                            temArr.push(mapcon);
                            MLayerObj[mapcon.ParentKEY] = [...temArr];
                        }
                        promises[key].then((dataSource) => {
                            dataSource.name = key;
                            viewer.dataSources.add(dataSource);
                            if (typeof (MChangeFun) == "function") {
                                MChangeFun(dataSource);
                            }
                        });
                    });
                
                  } catch (e) {
                    console.log('加载json数据失败',e)
                  }
            }
            if (typeof (callback) == "function") {
                callback(MLayerObj);
            }
        } catch (e) {
            console.log("topojson数据加载失败：", e);
        }
    }


    EntityByMaterial = (itementity) => {
        if (itementity.properties && itementity.properties.material) {
            let props = itementity.properties.getValue("");
            if (itementity.polygon) {
                itementity.polygon.outline = false;
                if (props.opcaity > -1 && props.opcaity < 101) {
                    let ta = 100 - props.opcaity;
                    ta = ta / 100;
                    itementity.polygon.material = Cesium.Color.fromCssColorString(props.material).withAlpha(ta);
                } else {
                    itementity.polygon.material = Cesium.Color.fromCssColorString(props.material);
                }

            } else if (itementity.polyline) {
                itementity.polyline.material = Cesium.Color.fromCssColorString(props.material);
            } else if (itementity.billboard) {
                let billb = new Cesium.BillboardGraphics({
                    image: props.material,
                    width: 12,
                    height: 12,
                });
                itementity.billboard = billb;
            }
        }
    }

    /**
     * @description 场景定位，定位到平台流域范围
     * @author tpy
     * @annotator hjj
     * @date 2020-06-03
     * @memberof SCesLayAdd
     */
    toCenter() {
        let maxLon = 113.8;
        let maxLat = 22.88;
        let minLon = 113.63;
        let minLat = 22.68;
        var ttrectangle = Cesium.Rectangle.fromDegrees(minLon, minLat, maxLon, maxLat);
        //石马河
        //var ttrectangle = Cesium.Rectangle.fromDegrees(113.972 , 22.675,114.240, 23.033);
        //yjw：海口定位
        //var ttrectangle = Cesium.Rectangle.fromDegrees(110.20, 19.90, 110.45, 20.07);
        window.CesiumViewer.scene.camera.setView({
            destination: ttrectangle,
            orientation: {
                heading: Cesium.Math.toRadians(0.0), //默认值
                pitch: Cesium.Math.toRadians(-90.0), // 默认值
                roll: 0.0 //默认值
            }
        });
    }
}
module.exports = SCesLayAdd;
// export default SCesLayAdd;